<?php
class ProductTypes extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organizations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product_types';
	}
	
	public function rules()
	{
		return array(
			array('type_description', 'required'),
			array('type_description', 'length', 'max'=>45),
		);
	}
		
	public function relations()
	{
		return array(
			'products'=>array(self::HAS_MANY, 'Products', 'product_type_ID'),
		);
	}
	
	public function getProductCategoryInfo()
	{
		$connection = Yii::app()->db;
		$sql = "SELECT * FROM ".$this->tableName();
		$command = $connection->createCommand($sql);
		$dataReader = $command->query();
		$res=array();
		foreach($dataReader as $row)
		{
			$key=$row['product_type_ID'];
			$res[$key]=$row['type_description'];
		}
		return $res;
	}
	
	public function getProductsByCategory($productTypeID)
	{
		$products=ProductTypes::model()->findByPk($productTypeID)->with('products')->products;
		$res=array();
		foreach($products as $product)
		{
			$key=$product->product_ID;
			$res[$key]=$product->product_name;
		}
		return $res;
	}
}
?>
